/************************************************************************************************
 * test examples of 100 interesting program in C
 * test 091.c
 * Guess Number
 ***********************************************************************************************/

#include <stdio.h>

#define NUMBER 3136
#define LENGTH 4

/*
 * guess a number which is kept by computer
 */

int main()
{
    int map[10] = {0};
    int i;
    int temp = NUMBER;
    int count = 0;

    // initiate map
    for (i = 0; i < LENGTH; i++)
    {
        map[temp%10] = 1;
        temp /= 10;
    }

    while (count < 20)
    {
        int guess_number;
        int temp_guess, temp_original;
        int right_digit = 0;
        int right_pos = 0;
        scanf("%d", &guess_number);
        printf("The Number is %d, Your Guess is %d, ", NUMBER, guess_number);
        if (guess_number == NUMBER)
        {
            printf("You Get It!!!\n");
            break;
        }
        temp_guess = guess_number;
        temp_original = NUMBER;
        for (i = 0; i < LENGTH; i++)
        {
            int digit_original = temp_original%10;
            int digit_guess = temp_guess%10;
            if (digit_original == digit_guess) right_pos++;
            if (map[digit_guess] == 1)
            {
                map[digit_guess] = 0;
                right_digit++;
            }
            temp_guess /= 10;
            temp_original /= 10;
        }
        // restore map
        // use original number to restore, NOT guess number!!!
        // guess number contains non-original digits
        temp_original = NUMBER;
        for (i = 0; i < LENGTH; i++)
        {
            map[temp_original%10] = 1;
            temp_original /= 10;
        }
        if (temp_guess > 0)
            printf("Your Input Is Too Big!!!\n");
        else
            printf("%d right digits, %d right positions.\n", right_digit, right_pos);
    }
}

